package org.amse.Archiver.model;
import java.io.*;
import java.util.ArrayList;

import org.amse.Archiver.algorithm.*;
import org.amse.Archiver.algorithm.huffman.*;
import org.amse.Archiver.view.*;
public class Model implements IModel{
    private IView myView;
    private File currentDir;
    private ArrayList<File> contentDir = new ArrayList<File>();
   
    public String toString(){
        String temp = null;
        temp = "CurrDir=" + currentDir + "\n";
        for(int i = 0; i < contentDir.size(); i++){
            temp = temp + "\t" + contentDir.get(i) + "\n";
        }
        return(temp);
    }

    public Model(IView view){
    	setMyView(view);
    	currentDir = new File(System.getProperty("user.dir"));  
        File[] fileList = currentDir.listFiles();
    	for(int i = 0; i < currentDir.list().length; i++){
            contentDir.add(fileList[i].getAbsoluteFile());
    	}
    }
    
    public void changeDir(int num) {
    	File curFile;
		if (num > 0 && num < contentDir.size() + 1) {
			curFile = contentDir.get(num - 1);
		} else if(num == 0){
			curFile = currentDir.getParentFile();
		} else {
			System.out.println(num);
			throw new IndexOutOfBoundsException();
		}
        if((curFile!=null) && (curFile.isDirectory())){
            setContent(curFile);
        } else if (curFile.getName().endsWith("huf")){
        	try {
				DataInputStream curInput = new DataInputStream(new FileInputStream(curFile));
				String name = curInput.readUTF();
				currentDir = curFile.getAbsoluteFile();
				contentDir.clear();
				contentDir.add(new File(name));
				
			} catch (IOException e) {
				e.printStackTrace();
			}
			
        }
    }

	private void setContent(File currDir){
		currentDir = currDir.getAbsoluteFile();
		File[] fileList = currentDir.listFiles();
		contentDir.clear();
		for(int i = 0; i < currDir.list().length; i++){
    	    contentDir.add(fileList[i].getAbsoluteFile());
    	}
	}
	
	public void compress(int num) throws IOException {
		File curFile = contentDir.get(num - 1);
		FileInputStream inFile = new FileInputStream(curFile);
        Statistic stat = new Statistic(inFile);		        
        stat.calcFrequency();
        inFile.close();
        int [] statArray = new int[stat.getSize()];
	    statArray = stat.getStatistic();
	    HuffmanTree tree = new HuffmanTree(statArray);
	    tree.buildTree();
	    CodeBuilder code = new CodeBuilder(tree);
	    ICoder codeFile = new DataCoderStream(curFile, code.getCode(), statArray, stat.getFileSize());
	    codeFile.encode();
	    setContent(currentDir);
	    myView.update();
	}
	
	public void decompress(int num) throws IOException{
		File curFile = contentDir.get(num - 1);
		if(!curFile.isDirectory() && curFile.getName().endsWith("huf")) {
			IDecoder decoder = new DataDecoderStream(curFile);
			decoder.decode();
		} 
		setContent(currentDir);			
		myView.update();
	}
	
	public ArrayList<File> getContentDir() {
		return contentDir;
	}
	
	public File getCurrentDir() {
		return currentDir;
	}

	public void setMyView(IView view) {
		myView = view;
	}
   
	public IView getMyView() {
		return myView;
	}
}
